<?php
// POSTS SHORTCODE by iKarina & iGor - http://www.mojo-themes.com/user/iKarina/
// You CAN NOT copy and use this code in your themes
function posts( $atts, $content = null ) {
	extract(shortcode_atts(array(
		'width'		=> '500',
		'height'		=> '',
		'autoplay'	=> '',
		'style'		=> 'default',
		'float'		=> '',
		'transition'=> '',
		'circular'	=> 'yes',
		'type'		=> 'recent',
		'view'		=> 'slider',
		'nav'			=> 'bottom',
		'cat'			=> '',
		'qty'			=> '3',
		'perslide'	=> '1',
		'orderby'	=> 'date',
		'title'		=> 'yes',
		'titletag'	=> 'strong',
		'date'		=> '',
		'postedin'	=> '',
		'tags'		=> '',
		'comments'	=> '',
		'excerpt'	=> '',
		'thumb'		=> 'none',
		'thumbw'		=> '',
		'thumbh'		=> ''
	), $atts));
	$slide = rand(9,9999);
	if ($perslide) {$iw = $width / $perslide - 7;};
	if ($width) {$w = 'width:'.$width.'px;'; $wpd = ($iw + 10) * $perslide;};
	if ($height) {$h = 'height:'.$height.'px;';};
	if ($float=="left") {$fl = 'fl';} elseif ($float=="right") {$fl = 'fr';};
	// MAIN WRAPPER
	if ($style=="default") {$wrapbefore = '<div class="wrapper-default '.$fl.'" style="'.$w.'">'; $wrapafter = '</div>';}
		elseif ($style=="white") {$wrapbefore = '<div class="wrapper-white '.$fl.'" style="'.$w.'">'; $wrapafter = '</div>';}
		elseif ($style=="gray") {$wrapbefore = '<div class="wrapper-gray '.$fl.'" style="'.$w.'">'; $wrapafter = '</div>';};
	// TRANSITION
	if ($transition=="fade") {$trattr = 'speed:1, onBeforeSeek: before, onSeek: after,'; $trfunc = 'function before() { this.getRoot().stop().css({ opacity: "0.3" });}; function after() { this.getRoot().stop().animate({ opacity: "1" },200);}; ';}
		elseif ($transition=="slide") {$trattr = 'speed:500,';}
		elseif ($transition=="fadingslide") {$trattr = 'speed:500, onBeforeSeek: before, onSeek: after,'; $trfunc = 'function before() { this.getRoot().stop().css({ opacity: "0.3" });}; function after() { this.getRoot().stop().animate({ opacity: "1" },200);}; ';}
		else {$trattr = 'speed:1,';};
	// AUTOPLAY + CIRCULAR SCROLLING
	if ($autoplay) {$autoscroll = ".autoscroll({ autoplay: true, interval: ".$autoplay." })";};
	if ($circular=="yes") {$circ = '.circular()';
		if (!($qty < $perslide)) {$indent = '$(".slider-'.$slide.'").find(".items").css({left:-'.$wpd.'}); ';};
	};
	// SLIDER WRAPPER
	if ($view == 'slider') {
		if ($nav == 'bottom') {$navbottom = '<ul class="tabs-dots pt10 tabs-slider-'.$slide.' none"></ul><span class="pt10 fr"><button class="nextbutton next'.$slide.'"><span>Next &gt;</span></button></span>';}
			elseif ($nav == 'top') {$navtop = '<ul class="tabs-dots pb10 tabs-slider-'.$slide.' none"></ul><span class="pb10 fr"><button class="nextbutton next'.$slide.'"><span>Next &gt;</span></button></span><div class="clear"><!-- --></div>';};
		$before = $wrapbefore.$navtop.'<div class="shortslider slider-'.$slide.'" style="'.$w.$h.'"><div class="items">';
		$after = '</div></div>'.$navbottom.'
			<div class="clear"><!-- --></div>'.$wrapafter.'
			<div>
			<script type="text/javascript">
			var q = $(".slider-'.$slide.'").find(".item").size();
			var liq = q/'.$perslide.';
				if((parseFloat(liq) == parseInt(liq))){
					var liqty = liq;} else {var liqty = liq + 1;
				};
				for (i=1; i<=liqty; i++) {
					$(".tabs-slider-'.$slide.'").append(\'<li><a href="#fragment-\' + i + \'">&nbsp;</a></li>\');
				;}
				if (liq == 1) {
					$(".tabs-slider-'.$slide.'").css("display","none");
					$(".tabs-slider-'.$slide.'").next().css("display","none");
				};
				$(".slider-'.$slide.'").scrollable({
					size:'.$perslide.',
					'.$trattr.'
					clickable: false,
					nextPage: ".next'.$slide.'"
				})'.$circ.'.navigator({
					navi: ".tabs-slider-'.$slide.'",
					naviItem: "li",
					activeClass: "tabs-dots-selected"
				})'.$autoscroll.';
				'.$trfunc.$indent.'
			</script>
			</div>
			<style type="text/css">.slider-'.$slide.' .items > div.item {width:'.$iw.'px;}</style>';
		$itembefore = '<div class="item ofposts"><div>';
		$itemafter = '</div></div>';
	// LIST WRAPPER
	} elseif ($view == 'list') {
		$before = $wrapbefore.'<ul class="shortlist '.$view.'">';
		$after = '</ul>'.$wrapafter;
		$itembefore = '<li>';
		$itemafter = '</li>';
	};
	// RECENT POSTS
	if ($type == 'recent') {
		$args = array(
			'numberposts' => $qty,
			'category' => $cat,
			'orderby' => $orderby
		);
		if ($view != 'slider') {
			$extra = '<a class="ico_pages" href="'.get_category_link($cat).'">All entries</a>';
		};
		$recentposts = get_posts($args);
		foreach($recentposts as $post) :
		setup_postdata($post);
			if ($title=="yes") {
					$posttitle = '<'.$titletag.' class="table"><a class="pb5 tcell lh13 ntd" href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a></'.$titletag.'>';
			};
			if ($date=="yes") {
					$time = '<div class="shortl">'.get_the_time('M jS, Y').'</div>';
			};
			if ($postedin=="yes") {
					$category = '<div class="shortl">Posted in: '.get_the_category_list(', ','', $post_id = $post->ID).'</div>';
			};
			if ($tags=="yes") {
					$tagbefore = '';
					$tag = '';
					$tagafter = '';
					$posttags = get_the_tags($post->ID);
					if ($posttags) {
						foreach($posttags as $tagg) {
							$tag .= '<a href="'.get_tag_link($tagg->term_id).'">'.$tagg->name.'</a>, ';
						}
						$tagbefore = '<div class="shortl">Tags: ';
						$tagafter = '</div>';
					}
			};
			if ($comments=="yes") {
					$comm = '<div class="shortl">Comments: <a href="'.get_comments_link($post->ID).'">'.get_comments_number($post->ID).'</a></div>';
			};
			if ($excerpt=="yes") {
					$exc = '<p>'.get_the_excerpt($post->ID).'</p><a href="'.get_permalink($post->ID).'" class="button">Read More</a>';
			};
			if ($thumbw || $thumbh) {
				// get a custom image of post
				$image2 = get_post_meta($post->ID, 'full_image_value', true);
				// get a first image from body of post
				ob_start();
				ob_end_clean();
				$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
				$fi = $matches [1] [0];
				// set a parameters of dimentions
				if ($thumbw) {$thumbwidth = '&amp;w='.$thumbw; $thw = 'width="'.$thumbw.'"';};
				if ($thumbh) {$thumbheight = '&amp;h='.$thumbh; $thh = 'height="'.$thumbh.'"';};
				//CHECK IMAGE POST
				if ($image2) {$thumbnail = '<a href="'.get_permalink($post->ID).'"><img class="thumb-'.$thumb.'" src="'.get_bloginfo('template_url').'/timthumb.php?src='.$image2.$thumbwidth.$thumbheight.'&amp;zc=1&amp;q=90" '.thw.thh.' alt="" /></a>';}
				//CHECK AUTOMATIC THUMB
				elseif ($fi<>"") {$thumbnail = '<a href="'.get_permalink($post->ID).'"><img class="thumb-'.$thumb.'" src="'.get_bloginfo('template_url').'/timthumb.php?src='.$fi.$thumbwidth.$thumbheight.'&amp;zc=1&amp;q=90" '.thw.thh.' alt="" /></a>';};
			};
			$out .= $itembefore.$thumbnail.$posttitle.$time.$category.$tagbefore.$tag.$tagafter.$comm.$exc.$itemafter;
		endforeach;
	};
	// RELATED POSTS
	if ($type == 'related') {
		$ts = wp_get_post_tags(get_the_ID());
		$tagsarray = array();
		foreach ($ts as $t) {
			$tagsarray[] = $t->term_id;
		};
		$args = array(
			'showposts' => $qty,
			'tag__in' => $tagsarray,
			'orderby' => $orderby
		);
		query_posts($args);
		while (have_posts()) : the_post();
			if ($title=="yes") {
					$posttitle = '<'.$titletag.' class="table"><a class="pb5 tcell lh13 ntd" href="'.get_permalink().'">'.get_the_title().'</a></'.$titletag.'>';
			};
			if ($date=="yes") {
					$time = '<div class="shortl">'.get_the_time('M jS, Y').'</div>';
			};
			if ($postedin=="yes") {
					$category = '<div class="shortl">Posted in: '.get_the_category_list(', ').'</div>';
			};
			if ($tags=="yes") {
					$tag = get_the_tag_list('<div class="shortl">Tags: ',', ','</div>');
			};
			if ($comments=="yes") {
					$comm = '<div class="shortl">Comments: <a href="'.get_comments_link().'">'.get_comments_number().'</a></div>';
			};
			if ($excerpt=="yes") {
					$exc = '<p>'.get_the_excerpt().'</p><a href="'.get_permalink().'" class="button">Read More</a>';
			};
			if ($thumbw || $thumbh) {
					$image2 = get_post_meta(get_the_ID(), 'full_image_value', true);
					if ($thumbw) {$thumbwidth = '&amp;w='.$thumbw; $thw = 'width="'.$thumbw.'"';};
					if ($thumbh) {$thumbheight = '&amp;h='.$thumbh; $thh = 'height="'.$thumbh.'"';};
					//CHECK IMAGE POST
					if ($image2) {$thumbnail = '<a href="'.get_permalink().'"><img class="thumb-'.$thumb.'" src="'.get_bloginfo('template_url').'/timthumb.php?src='.$image2.$thumbwidth.$thumbheight.'&amp;zc=1&amp;q=90" '.thw.thh.' alt="" /></a>';}
					//CHECK AUTOMATIC THUMB
						elseif (catch_that_image()<>"") {$thumbnail = '<a href="'.get_permalink().'"><img class="thumb-'.$thumb.'" src="'.get_bloginfo('template_url').'/timthumb.php?src='.catch_that_image().$thumbwidth.$thumbheight.'&amp;zc=1&amp;q=90" '.thw.thh.' alt="" /></a>';}
					//EMPTY THUMB
					else {$thumbnail = '<a href="'.get_permalink().'"><img class="thumb-'.$thumb.'" src="'.get_bloginfo('template_url').'/timthumb.php?src='.get_bloginfo('template_url').'/images/thumb_empty2.jpg'.$thumbwidth.$thumbheight.'&amp;zc=1&amp;q=90" '.thw.thh.' alt="" /></a>';};
			};
			$out .= $itembefore.$thumbnail.$posttitle.$time.$category.$tag.$comm.$exc.$itemafter;
		endwhile; wp_reset_query();
	};
	// RETURN POSTS
	return $before.$out.$extra.$after;
}
add_shortcode('posts', 'posts');
?>